package com.example.spring3.bank;


import java.math.BigDecimal;
import java.util.List;

public class OrderService {

    private List<GoodsCoupon> goodsCouponList;
    private OrderCoupon orderCoupon;

    public OrderService(List<GoodsCoupon> goodsCouponList, OrderCoupon orderCoupon) {
        this.goodsCouponList = goodsCouponList;
        this.orderCoupon = orderCoupon;
    }

    //计算订单价格
    private OrderInfo calculatePrice(List<Goods> goodsList) throws Exception {
        checkGoods(goodsList);
        OrderInfo orderInfo = new OrderInfo();
        Double price = 0D;
        for (int i = 0; i < goodsList.size(); i++) {
            Goods goods = goodsList.get(i);
            //保留两位小数
            Double f1 = goods.getNowPrice().setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); //保留2位小数
            //获取单个商品的优惠信息
            GoodsCoupon goodsCoupon = getGoodsCoupon(this.goodsCouponList, goods);
            ///单位*单价-单位*优惠=价钱
            Double v;
            if (goodsCoupon != null) {
                v = f1 * goods.getNowPrice().doubleValue() - f1 * goodsCoupon.getCouponPrice().doubleValue();
            } else {
                v = f1 * goods.getNowPrice().doubleValue();
            }
            //计算总价
            System.out.println(v);
            price = price + v;
        }
        //获取订单的优惠信息
        BigDecimal bigDecimal = new BigDecimal(price);
        Double totalPrice = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); //保留2位小数
        OrderCoupon orderCoupon = getOrderCoupon(new BigDecimal(totalPrice));
        //订单优惠之后的价钱
        double couponAfterPrice = price - orderCoupon.getCouponPrice().doubleValue();
        Double afterPrice = new BigDecimal(couponAfterPrice).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); //保留2位小数;
        orderInfo.setOrderTotalPrice(afterPrice);
        return orderInfo;
    }

    private OrderCoupon getOrderCoupon(BigDecimal price) {
        if (this.orderCoupon != null && price.compareTo(this.orderCoupon.getMinPrice()) == 1) {
            return this.orderCoupon;
        } else {
            OrderCoupon orderCoupon = new OrderCoupon();
            //不满足条件的优惠是0
            orderCoupon.setCouponPrice(new BigDecimal(0));
            return orderCoupon;
        }
    }

    //从商品的优惠信息中找到商品优惠价钱
    private GoodsCoupon getGoodsCoupon(List<GoodsCoupon> goodsCouponList, Goods goods) {
        GoodsCoupon coupon = null;
        if (goodsCouponList != null) {
            for (int i = 0; i < goodsCouponList.size(); i++) {
                GoodsCoupon goodsCoupon = goodsCouponList.get(i);
                if (goodsCoupon.getGoodsId().equals(goods.getId())) {
                    if (goodsCoupon.isDiscount()) {
                        double v = goods.getNowPrice().doubleValue() * goodsCoupon.getDiscountPercent();
                        double v1 = v / 100;
                        goodsCoupon.setCouponPrice(goods.getNowPrice().subtract(new BigDecimal(v1)));
                        coupon = goodsCoupon;
                    } else {
                        coupon = goodsCoupon;
                    }
                    break;
                }
            }
        }
        return coupon;

    }


    //检查商品是否符合下单的要求
    private void checkGoods(List<Goods> goodsList) throws Exception {
        for (int i = 0; i < goodsList.size(); i++) {
            Goods goods = goodsList.get(i);
            if (Math.abs(goods.getKg().doubleValue()) <= 0 || Math.abs(goods.getNowPrice().doubleValue()) <= 0) {
                throw new Exception("商品单位数量或者价格不能小于0");
            }
        }
    }


    public OrderInfo createOrder(List<Goods> goodsList) throws Exception {
        OrderInfo orderInfo = calculatePrice(goodsList);
        return orderInfo;
    }


}
